#ifndef _L298N_H
#define _L298N_H

#include "sys.h"
#include "usart.h"
#include "string.h"

// L298N 电机驱动——基于 STM32F407 开发板	      

/* 车轮 */
#define Wheel       	GPIOA					//车轮驱动端口
#define Wheel_Clock 	RCC_AHB1Periph_GPIOA	//车轮端口时钟总线
#define Left_0	   	 	GPIO_Pin_0				//左轮
#define Left_1			GPIO_Pin_1
#define Right_0    		GPIO_Pin_2				//右轮
#define Right_1    		GPIO_Pin_3

/* 两路 PWN */
#define PWN0_GPIO_PORT                GPIOF
#define PWN0_GPIO_CLK                 RCC_AHB1Periph_GPIOF
#define PWN0_GPIO_PIN                 GPIO_Pin_7
#define PWN0_GPIO_PINSOURCE           GPIO_PinSource7
#define PWN0_GPIO_TIM_AF         	  GPIO_AF_TIM11

#define PWN0_TIM                 	  TIM11
#define PWN0_TIM_CLK                  RCC_APB2Periph_TIM11

#define PWN1_GPIO_PORT                GPIOF
#define PWN1_GPIO_CLK                 RCC_AHB1Periph_GPIOF
#define PWN1_GPIO_PIN                 GPIO_Pin_9
#define PWN1_GPIO_PINSOURCE           GPIO_PinSource9
#define PWN1_GPIO_TIM_AF         	  GPIO_AF_TIM14

#define PWN1_TIM                      TIM14
#define PWN1_TIM_CLK                  RCC_APB1Periph_TIM14

/* PWN 频率 */
//1000为一个周期，占空比值越大，速度越快  频率=84M/84=1MHZ
#define APB1_Prescaler		(168 - 1)
#define APB2_Prescaler		(84 - 1)
#define Tim_Period			(1000 - 1)
// PWN 输出电平极性
#define Low					0
#define Hight				1
//初始速度
#define SPEED_VALUE			200

/* 小车行驶状态 | 行驶命令 */
#define GO   				11
#define BACK   				22
#define STOP       			33
/* 转向命令 */
#define Turn_Left   		44
#define Turn_Right  		55

/* 车轮转动方向 */
//左轮向前转
#define Left_Forward      	GPIO_ResetBits(Wheel, Left_0);\
	                        GPIO_SetBits(Wheel, Left_1)
//左轮向后转
#define Left_Backward		GPIO_ResetBits(Wheel, Left_1);\
							GPIO_SetBits(Wheel, Left_0)
//右轮向前转
#define Right_Forward     	GPIO_ResetBits(Wheel, Right_1);\
	                        GPIO_SetBits(Wheel, Right_0)
//右轮向后转
#define Right_Backward    	GPIO_ResetBits(Wheel, Right_0);\
	                        GPIO_SetBits(Wheel, Right_1)
//制动
#define Stop              	GPIO_ResetBits(Wheel, Left_0);\
	                        GPIO_ResetBits(Wheel, Left_1);\
                            GPIO_ResetBits(Wheel, Right_0);\
	                        GPIO_ResetBits(Wheel, Right_1)

void L298N_Init(void);//初始化
u16 L298N_Speed_Set(u16 PWN_Value);//调速
int L298N_Change_Status(int status, int cmd);//转向

#endif
